package com.steven.ourcontest.information.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.steven.ourcontest.R
import com.steven.ourcontest.information.bean.UserDetail
import com.steven.ourcontest.net.NetConstants
import com.steven.ourcontest.utils.Identity

class AdminManageAdapter(
    private val context: Context,
    private val onPassClick:((uid:Long)->Unit),
    private val onRefuseClick:((uid:Long)->Unit)
) : RecyclerView.Adapter<ManageViewHolder>() {

    val itemData by lazy {
        arrayListOf<UserDetail>()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ManageViewHolder {
        val inflater =
            LayoutInflater.from(context).inflate(R.layout.item_manage_identity, parent, false)
        val layoutParams = inflater.layoutParams as RecyclerView.LayoutParams
        layoutParams.width = RecyclerView.LayoutParams.MATCH_PARENT
        layoutParams.height = RecyclerView.LayoutParams.MATCH_PARENT
        inflater.layoutParams = layoutParams
        return ManageViewHolder(inflater)
    }

    override fun getItemCount(): Int {
        return itemData.size
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ManageViewHolder, position: Int) {
        val uid = itemData[position].uid
        holder.apply {
            tv_uid?.text = "UID=${uid}"
            tv_user_name?.text = "姓名：${itemData[position].uname}"
            tv_school_name?.text = "学校：${itemData[position].school}"
            tv_college_name?.text = "学院：${itemData[position].college}"
            tv_judge_name?.text = "专业：${itemData[position].judge}"
            tv_year?.text = "入学年份：${itemData[position].startYear}"
            tv_schoolType?.text = "学历：${itemData[position].schoolType}"
            tv_userType?.text = "认证身份：${when (itemData[position].userType) {
                Identity.TEACHER.value -> {
                    "教师"
                }
                Identity.STUDENT.value -> {
                    "学生"
                }
                else -> {
                    "数据发生错误"
                }
            }
            }"
            iv_image?.let {
                Glide.with(context)
                    .load(NetConstants.ICON_BASE_URL+itemData[position].imageUrl)
                    .placeholder(R.drawable.ic_placeholder)
                    .dontAnimate()
                    .error(R.drawable.aio_image_fail)
                    .into(it)
            }
            tv_pass?.setOnClickListener {
                onPassClick.invoke(uid)
            }
            tv_refuse?.setOnClickListener {
                onRefuseClick.invoke(uid)
            }
        }
    }
}

class ManageViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    var tv_uid: TextView? = itemView.findViewById(R.id.tv_uid)
    var tv_user_name: TextView? = itemView.findViewById(R.id.tv_user_name)
    var tv_school_name: TextView? = itemView.findViewById(R.id.tv_school_name)
    var tv_college_name: TextView? = itemView.findViewById(R.id.tv_college_name)
    var tv_judge_name: TextView? = itemView.findViewById(R.id.tv_judge_name)
    var tv_year: TextView? = itemView.findViewById(R.id.tv_year)
    var tv_schoolType: TextView? = itemView.findViewById(R.id.tv_schoolType)
    var tv_userType: TextView? = itemView.findViewById(R.id.tv_userType)
    var iv_image: ImageView? = itemView.findViewById(R.id.iv_image)
    var tv_pass: TextView? = itemView.findViewById(R.id.tv_pass)
    var tv_refuse: TextView? = itemView.findViewById(R.id.tv_refuse)
}